//
//  SecondViewController.m
//  tradersOnMap
//
//  Created by bilmuh bilmuh on 07.03.2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SoldItemsViewController.h"


@implementation SoldItemsViewController

-(void)setup
{
	UIImage *image = [UIImage imageNamed:@"shoppingCart.png"];
	UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"My Items" image:image tag:0];
	self.tabBarItem = item;
	[item release], [image release];
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
	self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
	if (self) {
		[self setup];
	}
	return self;
}

-(void)awakeFromNib
{
	[self setup];
}

-(GetItemBrain *)brain
{
	if (!brain) {
		brain = [[GetItemBrain alloc] init];
	}
	return brain;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	[[self brain] getItems];
	return [[self brain].items count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
	Item *item = (Item *)[[self brain].items objectAtIndex:indexPath.row];
    UIImage *image = [UIImage imageWithData:[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", item.imageURL]]]];

	cell.imageView.image = image;
	cell.textLabel.text = item.itemTitle;
	cell.detailTextLabel.text = item.description;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	
	return [cell autorelease];
}

-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
	NSUserDefaults *def = [[NSUserDefaults alloc] init];
	Item *item = (Item *)[[self brain].items objectAtIndex:indexPath.row];
	[def setObject:item.itemID forKey:@"itemID"];
	[def release];
	
	ItemDetailViewController *vc = [[ItemDetailViewController alloc] init];
	[self.navigationController pushViewController:vc animated:YES];
	[vc release];
}


@end
